var router = require('express').Router();
var mailer = require('../components/backend/service').mailer;
var util = require('../components/backend/util'),
    log = util.log;

module.exports = function () {

  router.get('/callback', function (req, res) {
    var mailToken = req.query.mailToken;
    mailer.handleCallback(mailToken).then(function (result) {
      if (result.certificated == true) {
        util.sandbox.emit(util.sandbox.MSG.MAIL_SUCCESS, {openid: result.openid});
        res.send('验证成功！');
      } else {
        res.send(result);
      }
    },function(err){
      log.errLogger.error(err);
    });
  });

  return router;
};

